
// https://stackoverflow.com/questions/41244144/link-object-files-from-header-files-in-real-mode-when-using-gcc-m16-option

#include "../../lib/real_mode/c/kprint.h"
#include "../../lib/real_mode/c/longlong.h"
#include "../../lib/real_mode/c/read_disk.h"
#include "../../lib/real_mode/c/rsa.h"
#include "../../lib/real_mode/c/sha256.h"

/**
 * @brief 将数据以hex的形式打印出来
 */
void print_hex( const uint8_t *buf, const size_t len )
{
    char text[] = { ", " };
    for ( size_t i = 0; i < len; i++ )
    {
        kprintf_int( *( buf + i ), sizeof( uint8_t ) );
        kprintf( text );
    }
    kprintf_nl( );
    return;
}

/*测试通过
bool crypto_test( unsigned char *buf, const unsigned long message_size )
{
    print_hex( buf, message_size );
    //获取公私密钥
    struct public_key_class pub;
    private_key_class priv;

    rsa_gen_keys( &pub, &priv );
    myuint64_t encrypted[ message_size ];
    bool flag = rsa_encrypt( buf, message_size, &pub, encrypted );
    if ( !flag )
    {
        char text[] = { "encrypt failed\n" };
        kprintf( text );
    }
    else
    {
        char text[] = { "encrypted\n" };
        kprintf( text );
        print_hex( ( uint8_t * ) encrypted, message_size * sizeof( long ) );
    }

    unsigned char decrypted[ message_size ];
    flag = rsa_decrypt( encrypted, message_size * sizeof( myuint64_t ), &priv,
                        decrypted );
    if ( !flag )
    {
        char text[] = { "decrypt failed\n" };
        kprintf( text );
    }

    else
    {
        char text2[] = { "\ndecrypted:\n" };
        kprintf( text2 );
        print_hex( decrypted, message_size );
    }

    return true;
}
//*/

void main( void )
{
    clearScreen( );
    setCursorPosition( 0, 0 );
    char text[] = { "I'm from pmbr\n" };
    kprintf( text );

    /*测试通过
    memset( ( void * ) 0x7c00, ( uint8_t ) 0, 512 );
    {
        char text[] = { "123abc" };
        memcpy( ( void * ) 0x7c00, text, 6 );
    }
    crypto_test( ( uint8_t * ) 0x7c00, 6 );
    //*/

    /*********************读取MBR************************/
    uint8_t status = 0;
    uint8_t sectors_read = 0;
    // 读取MBR代码成功
    read_disk( 1, 0, 1, 0, 0x81, ( uint8_t * ) 0x7c00, &status, &sectors_read );

    /*************************计算MBR哈希值***************************/
    //校验MBR
    //计算MBR的哈希值
    BYTE buf[ SHA256_BLOCK_SIZE ];
    memset( buf, ( uint8_t ) 0, SHA256_BLOCK_SIZE );
    sha256_hash( buf, ( unsigned char * ) 0x7c00, 512 );
    {
        char text[] = { "\nhash of MBR:\n" };
        kprintf( text );
        print_hex( buf, SHA256_BLOCK_SIZE );
    }

    /**************************对MBR的哈希值进行加密*************************/
    //获取公私密钥
    struct public_key_class pub;
    private_key_class priv;
    const size_t message_size = SHA256_BLOCK_SIZE;
    myuint64_t encrypted[ message_size ];
    unsigned char decrypted[ message_size ];

    rsa_gen_keys( &pub, &priv );
    {

        bool flag = rsa_encrypt( buf, message_size, &pub, encrypted );
        if ( !flag )
        {
            char text[] = { "encrypt failed\n" };
            kprintf( text );
        }
        else
        {
            char text[] = { "\nencrypt hash of MBR:\n" };
            kprintf( text );
            print_hex( ( uint8_t * ) encrypted, message_size * sizeof( long ) );
        }
    }

    /*************************对MBR的哈希值进行解密***************************/
    {
        bool flag = rsa_decrypt( encrypted, message_size * sizeof( myuint64_t ),
                            &priv, decrypted );
        if ( !flag )
        {
            char text[] = { "decrypt failed\n" };
            kprintf( text );
        }

        else
        {
            char text[] = { "\ndecrypted of the MBR's hash:\n" };
            kprintf( text );
            print_hex( decrypted, message_size );
        }
    }

    // __asm__("jmp 0x7c00");

    while ( 1 )
    {
    }
}
